package com.yc.jpyy.mobile.controller;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.dao.BizDao;
import com.yc.jpyy.mobile.service.BizService;

@Controller
@RequestMapping(value = "/PreRegister")
public class AddPreRegistrationInfo extends BaseController {
	
	@Autowired
	private BizService bizService;

	@Autowired
	private BizDao bizDao;
	private static Logger logger = LoggerFactory.getLogger(GetTeacherListInfo.class);
	
	/**
	 * 报名意向添加
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/AddPreRegistration", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String AddPreRegistration(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 学员名字
			String name = request.getParameter("name");
			// 联系电话
			String mobilephone = request.getParameter("mobilephone");
            //户籍类型
			String houseHoldType = request.getParameter("houseHoldType");
			// 班型编号
			String classinfoid = request.getParameter("classinfoid");
			//教练ID
			String teacherid = request.getParameter("teacherid");
			// 备注
			String remark = request.getParameter("remark");
			
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
			if(mobilephone != null && !mobilephone.isEmpty() 
					&& name != null && !name.isEmpty()
					&& null != houseHoldType && !houseHoldType.isEmpty())
			{
				Map<String, Object> valMap = new HashMap<String, Object>();
				valMap.put("name", name);
				valMap.put("mobilephone", mobilephone);
				valMap.put("houseHoldType", Integer.parseInt(houseHoldType));
				valMap.put("classinfoid", Integer.parseInt(classinfoid));
				valMap.put("teacherid", teacherid);
				valMap.put("remark", remark);
				valMap.put("createtime", df.format(new Date()));
				//添加报名意向
				response = bizService.addPreRegister(valMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("姓名或联系电话或户籍类型不能为空...");
			}
		} catch (Exception e) {
		    logger.info("AddPreRegistration:"+ e.toString());
			response.setReturnMsg(1,"添加报名意向信息失败！");
		} finally {
			return response.toJSON();
		}
	}
}
